/*
* Copyright 2014 Bvin H.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.bvin.stickyviewpagerheaders.library.delegates;

import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class ScollViewDelegate implements ViewDelegate{

	private final int[] mViewLocationResult = new int[2];
    private final Rect mRect = new Rect();
    
	public boolean isViewBeingDragged(MotionEvent event, ScrollView view) {
		
        view.getLocationOnScreen(mViewLocationResult);
        final int viewLeft = mViewLocationResult[0], viewTop = mViewLocationResult[1];
        mRect.set(viewLeft, viewTop, viewLeft + view.getWidth(), viewTop + view.getHeight());
        final int rawX = (int) event.getRawX(), rawY = (int) event.getRawY();

        if (mRect.contains(rawX, rawY)) {
            return isReadyForPull(view, rawX - mRect.left, rawY - mRect.top);
        }

        return false;
    }
	
	@Override
	public boolean isReadyForPull(View view, float x, float y) {
		ScrollView scrollView = (ScrollView) view;
		return scrollView.getScrollY()==0;
	}

}
